package com.flexcodejam.vfd.command {

	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;

	import com.flexcodejam.vfd.business.*;
	import com.flexcodejam.vfd.control.*;
	import com.flexcodejam.vfd.event.*;
	import com.flexcodejam.vfd.model.*;
	import com.flexcodejam.vfd.vo.*;

	import mx.rpc.IResponder;
	import mx.rpc.events.*;
	import mx.controls.Alert;
	import mx.utils.ObjectUtil;

	public class LoginUserCommand implements ICommand, IResponder
	{
		private var model:VFDModelLocator = VFDModelLocator.getInstance();

		public function LoginUserCommand():void
		{
			// constructor...
		}

		public function execute(event:CairngormEvent):void
		{

			var delegate:AuthenticationDelegate = new AuthenticationDelegate(this);
			var userEvent:UserEvent = event as UserEvent;

			delegate.Login(userEvent.user);
		}


		public function result(event:Object):void
		{
			Alert.show(ObjectUtil.toString(event.result));

			model.currentUser = event.result as User;
			
			
			if(model.currentUser.ID > 0){
				var ev:ChangeMainViewStackEvent = new ChangeMainViewStackEvent(ChangeMainViewStackEvent.GO_TO_NEW_EDIT_FOOD_DRIVE,true,false);
		 		CairngormEventDispatcher.getInstance().dispatchEvent(ev);
				model.editFoodDrive_User = model.currentUser;
				
				
			}else{	//Go to Register
				var ev1:ChangeMainViewStackEvent = new ChangeMainViewStackEvent(ChangeMainViewStackEvent.GO_TO_NEW_EDIT_FOOD_DRIVE,true,false);
		 		CairngormEventDispatcher.getInstance().dispatchEvent(ev1);
			}
		}

		public function fault(event:Object):void{
			// display the error message to the user
			Alert.show(ObjectUtil.toString(event.toString()).substring(1,600), "Error");
		}
	}
}

